﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;

internal class SerializedFileContainerPreparer
{
    private static string _stagingAreaData;
    [CompilerGenerated]
    private static Func<string, SerializedFileContainerWriter.SerializedFile> <>f__am$cache1;
    [CompilerGenerated]
    private static Func<string, string> <>f__am$cache2;

    public static void CreateSerializedFileContainer(FlashFileHelper flashFileHelper)
    {
        _stagingAreaData = flashFileHelper.StagingArea;
        if (<>f__am$cache1 == null)
        {
            <>f__am$cache1 = f => GetSerializedFile(f);
        }
        SerializedFileContainerWriter.SerializedFile[] files = Enumerable.Select<string, SerializedFileContainerWriter.SerializedFile>(FindFilesThatShouldBeEmbedded(), <>f__am$cache1).ToArray<SerializedFileContainerWriter.SerializedFile>();
        SerializedFileContainerWriter.Write(_stagingAreaData, files, flashFileHelper.PathForAs3Src());
    }

    private static IEnumerable<string> FindFilesThatShouldBeEmbedded()
    {
        <FindFilesThatShouldBeEmbedded>c__AnonStorey35 storey = new <FindFilesThatShouldBeEmbedded>c__AnonStorey35();
        List<string> list = new List<string> { "mainData", "Resources/unity default resources" };
        storey.patterns = new string[] { @"\.assets$", "level", "PlayerConnectionConfigFile" };
        if (<>f__am$cache2 == null)
        {
            <>f__am$cache2 = f => Path.GetFileName(f);
        }
        list.AddRange(Enumerable.Select<string, string>(Enumerable.Where<string>(Directory.GetFiles(_stagingAreaData), new Func<string, bool>(storey.<>m__53)), <>f__am$cache2));
        return list;
    }

    private static SerializedFileContainerWriter.SerializedFile GetSerializedFile(string file)
    {
        long length = new FileInfo(_stagingAreaData + "/" + file).Length;
        return new SerializedFileContainerWriter.SerializedFile { Name = file, FileName = GetTextPlaceHolderForFile(file), FileSize = length };
    }

    private static string GetTextPlaceHolderForFile(string fileName)
    {
        string str = Environment.CurrentDirectory + "/" + _stagingAreaData + "/";
        UTF8Encoding encoding = new UTF8Encoding();
        string str2 = fileName + "_txt";
        string s = "_UNITY_REPLACEFORFILE__" + str + fileName;
        using (FileStream stream = new FileStream(_stagingAreaData + "/" + str2, FileMode.CreateNew))
        {
            stream.Write(encoding.GetBytes(s), 0, encoding.GetByteCount(s));
        }
        return fileName;
    }

    private static bool MatchesFilter(string input, IEnumerable<string> patterns)
    {
        <MatchesFilter>c__AnonStorey36 storey = new <MatchesFilter>c__AnonStorey36 {
            input = input
        };
        return Enumerable.Any<string>(patterns, new Func<string, bool>(storey.<>m__55));
    }

    [CompilerGenerated]
    private sealed class <FindFilesThatShouldBeEmbedded>c__AnonStorey35
    {
        internal string[] patterns;

        internal bool <>m__53(string ps)
        {
            return SerializedFileContainerPreparer.MatchesFilter(ps, this.patterns);
        }
    }

    [CompilerGenerated]
    private sealed class <MatchesFilter>c__AnonStorey36
    {
        internal string input;

        internal bool <>m__55(string p)
        {
            return Regex.Match(this.input, p).Success;
        }
    }
}

